#include "../include/head.h"
 // 创建用户信息链表
 UserList *create_user_list()
 {
     UserList *list = (UserList *)malloc(sizeof(UserList));
     if (list == NULL) {
         perror("无法分配内存");
         exit(EXIT_FAILURE);
     }
     list->head = NULL;
     list->size = 0;
     return list;
 }

 // 添加用户信息
void add_user(UserList *list, User user)
{
    UserNode *new_node = (UserNode *)malloc(sizeof(UserNode));
    if (new_node == NULL) {
        perror("无法分配内存");
        exit(EXIT_FAILURE);
    }
    new_node->user = user;
    new_node->next = list->head;
    list->head = new_node;
    list->size++;
}

 //销毁用户信息链表
 void destroy_user_list(UserList **list)
    {
        UserNode *current = (*list)->head;
        UserNode *next_node;
        while (current != NULL) {
            next_node = current->next;
            free(current);
            current = next_node;
        }
        free(*list);
        *list = NULL;
    }

//创建商品信息链表
CommodityList *create_commodity_list()
{
    CommodityList *list = (CommodityList *)malloc(sizeof(CommodityList));
    if (list == NULL) {
        perror("无法分配内存");
        exit(EXIT_FAILURE);
    }
    list->head = NULL;
    list->size = 0;
    return list;
}

//添加商品信息
void add_commodity(CommodityList *list, Commodity commodity)
{
    CommodityNode *new_node = (CommodityNode *)malloc(sizeof(CommodityNode));
    if (new_node == NULL) {
        perror("无法分配内存");
        exit(EXIT_FAILURE);
    }
    new_node->commodity = commodity;
    new_node->next = list->head;
    list->head = new_node;
    list->size++;
}

//销毁商品信息链表
void destroy_commodity_list(CommodityList **list)
{
    CommodityNode *current = (*list)->head;
    CommodityNode *next_node;
    while (current != NULL) {
        next_node = current->next;
        free(current);
        current = next_node;
    }
    free(*list);
    *list = NULL;
}

//创建购物车清单列表
CommodityList *create_shopping_cart_list()
{
    CommodityList *list = (CommodityList *)malloc(sizeof(CommodityList));
    if (list == NULL) {
        perror("无法分配内存");
        exit(EXIT_FAILURE);
    }
    list->head = NULL;
    list->size = 0;
    return list;
}

//添加购物车清单商品信息
void add_shopping_cart_commodity(CommodityList *list, Commodity commodity)
{
    CommodityNode *new_node = (CommodityNode *)malloc(sizeof(CommodityNode));
    if (new_node == NULL) {
        perror("无法分配内存");
        exit(EXIT_FAILURE);
    }
    new_node->commodity = commodity;
    new_node->next = list->head;
    list->head = new_node;
    list->size++;
}

//销毁购物车清单列表
void destroy_shopping_cart_list(CommodityList **list)
{
    CommodityNode *current = (*list)->head;
    CommodityNode *next_node;
    while (current != NULL) {
        next_node = current->next;
        free(current);
        current = next_node;
    }
    free(*list);
    *list = NULL;
}